#!/bin/bash
encodeurl() {
	local length="${#1}"
	for (( i = 0; i < length; i++))
	do  
		local char="${1:$i:1}"
		case $char in 
			[a-zA-Z0-9.~_-:*#/\\]) # 不需要转码的字符
				printf '%s' "$char" ;;
			*) 
				echo "$char" | tr -d '\n' | xxd -plain | sed 's/\(..\)/%\1/g' | tr -d '\n' | tr '[a-z]' '[A-Z]';;
		esac
	done
}

dir=`echo $@ | awk -F \\\\ '{printf "%s",$(NF-1)}'`
file=`echo $@ | awk -F \\\\ '{printf "%s",$(NF)}'`

# 得到dir中的中文,如果为空则不用进行转码
zh_str=$(echo "${dir}" | awk '{print gensub(/[!-~]/,"","g",$0)}')
if [ "x${zh_str}" = "x" ]
then
	echo "no chinese character"
	echo ${dir}  | iconv -f GBK -t UTF-8  > ./tmp_file_path.txt
else
	# 对中文和空格等进行转码ASCII
	url=`encodeurl "${dir}"`
	# 输出结果到临时文件,用于bat脚本读取生成url
	echo ${url}  | iconv -f GBK -t UTF-8  > ./tmp_file_path.txt
fi

git add "./images/${dir}/${file}"
git commit -m "add image ./${dir}/${file}"
git push
